<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// The Pligg Team <pligger at pligg dot com>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

	global $new_search, $cached_links, $current_user;
	$link = new Link;

	if ($new_search) {
		$rows = $new_search['count'];
		$new_search = $new_search['rows'];
		$the_results = $new_search;
	} else {
		$rows = $db->get_var($linksum_count);
		$links = $db->get_col($linksum_sql);
		$the_results = $links;
	}
	
	if($the_results){
		// find out if the logged in user voted / reported each of
		// the stories that the search found and cache the results
		require_once(mnminclude.'votes.php');
		$vote = new Vote;
		$vote->type='links';
		$vote->user=$current_user->user_id;
		$vote->link=$the_results;
		$results = $vote->user_list_all_votes();
		$vote = '';
		$results = ''; // we don't actually need the results 
				// we're just calling this to cache the results
				// so when we foreach the links we don't have to 
				// run 1 extra query for each story to determine
				// current user votes
  
		// setup the link cache
		$i = 0;
		// if this query changes also change it in the read() function in /libs/link.php
		$sql = "SELECT " . table_links . ".* FROM " . table_links . " WHERE "; 
		foreach($the_results as $link_id) {
			// first make sure we don't already have it cached
			if(!isset($cached_links[$link_id])){
				if ($i > 0){$sql .= ' OR ';}
				$sql .= " link_id = $link_id ";
				$i = $i + 1;
			}
		}
  		
		// if $i = 0 then all the links are already cached
		// so don't touch the db
		// if $i > 0 then there is at least 1 link to get
		// so get the SQL and add results to the cache
		if ($i > 0){
			$results = $db->get_results($sql);

			// add the results to the cache  
			foreach ($results as $row){
				$cached_links[$row->link_id] = $row;
			}
		}
		// end link cache setup
	}



		if ($new_search) {
			foreach($new_search as $link_id) {
				$link->id=$link_id;
				$link->read();
				$link->print_summary('summary');
			}
	} else {
	if ($links) {
		foreach($links as $link_id) {
			$link->id=$link_id;
			$link->read();
			$link->print_summary('summary');
			}
		}
	}
?>